load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test")

package(default_visibility = ["//rs:ic-os-pkg"])

DEPENDENCIES = [
    # Keep sorted.
    ":build_script",
    "//rs/ic_os/config_types",
    "//rs/ic_os/deterministic_ips",
    "//rs/ic_os/device",
    "//rs/ic_os/grub",
    "//rs/ic_os/metrics_tool",
    "//rs/ic_os/sev",
    "@crate_index//:anyhow",
    "@crate_index//:askama",
    "@crate_index//:clap",
    "@crate_index//:devicemapper",
    "@crate_index//:gpt",
    "@crate_index//:macaddr",
    "@crate_index//:memchr",
    "@crate_index//:nix",
    "@crate_index//:regex",
    "@crate_index//:systemd",
    "@crate_index//:tempfile",
    "@crate_index//:thiserror",
    "@crate_index//:tokio",
    "@crate_index//:tokio-util",
    "@crate_index//:uuid",
    "@crate_index//:virt",
    "@libvirt",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/ic_os/build_tools/partition_tools",
    "@crate_index//:goldenfile",
    "@crate_index//:url",
]

MACRO_DEPENDENCIES = []

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    data = ["templates/guestos_vm_template.xml"],
)

rust_binary(
    name = "guest_vm_runner",
    srcs = glob(["src/**/*.rs"]),
    crate_name = "guest_vm_runner",
    proc_macro_deps = MACRO_DEPENDENCIES,
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    deps = DEPENDENCIES + ["//rs/ic_os/config:config_lib"],
)

rust_binary(
    name = "guest_vm_runner_dev",
    srcs = glob(["src/**/*.rs"]),
    crate_features = ["dev"],
    crate_name = "guest_vm_runner",
    proc_macro_deps = MACRO_DEPENDENCIES,
    target_compatible_with = [
        "@platforms//os:linux",
    ],
    deps = DEPENDENCIES + ["//rs/ic_os/config:config_lib_dev"],
)

# You can update the XML config golden files with
# bazel test //rs/ic_os/os_tools/guest_vm_runner:guest_vm_runner_test --test_env=UPDATE_GOLDENFILES=1 --spawn_strategy=local --test_arg=guest_vm_config
rust_test(
    name = "guest_vm_runner_test",
    crate = ":guest_vm_runner_dev",
    crate_features = ["dev"],
    data = glob(["golden/*"]),
    deps = DEV_DEPENDENCIES,
)

rust_test(
    name = "guest_vm_runner_integration_tests",
    # the test sometimes times out on CI with default timeout
    # of "moderate" (5 minutes) - 2025-07-04
    timeout = "long",
    crate = ":guest_vm_runner_dev",
    crate_features = [
        "skip_default_tests",
        "dev",
        "integration_tests",
    ],
    data = glob(["golden/*"]) + ["//ic-os/guestos/envs/prod:disk-img-for-tests.tar"],
    env = {"ICOS_IMAGE": "$(rootpath //ic-os/guestos/envs/prod:disk-img-for-tests.tar)"},
    flaky = True,
    # Mark it manual here and expose it in //ic-os/tests:integration_tests
    tags = ["manual"],
    deps = DEV_DEPENDENCIES,
)

rust_test(
    name = "upgrade_device_mapper_test",
    crate = ":guest_vm_runner_dev",
    crate_features = [
        "skip_default_tests",
        "dev",
        "upgrade_device_mapper_test",
    ],
    # This test needs root, so we mark it manual here and expose it in //rs/tests/node:root_tests
    tags = ["manual"],
    visibility = ["//rs/tests/node:__subpackages__"],
    deps = DEPENDENCIES + DEV_DEPENDENCIES,
)
